import openpyxl
import re
import torch
# import  torch.nn.functional as F
from torch.nn import functional as F
import jieba

TypeTable = ['prefix', 'None', '召喚物', '抗性', '光環', '混沌', '能量護盾', '寶石', '火焰', '詛咒', '護甲', '暴擊',
             '冰冷', '閃避', '物理', '生命', '攻擊', '傷害', '魔力', '元素', '速度', '閃電', '防禦', '異常狀態', '法術',
             '能力']

# 打开xlsx文件
workbook = openpyxl.load_workbook('cizhui.xlsx')

# 选择活动工作表
sheet = workbook.active

# # 读取单元格的值
# cell_value = sheet['A1'].value
# print(cell_value)

# # 读取整行和整列的值
# row_values = [cell.value for cell in sheet[1]]  # 第一行的值
# print(row_values)
#
# column_values = [sheet[f'A{i}'].value for i in range(1, sheet.max_row + 1)]  # 第一列的值
# print(column_values)

# 遍历所有的行和列
all_content = []


def replace_numbers(match):
    # print(match.group())
    return "number"


def change(content):
    pattern = r'\(\d+-\d+\)'
    new_content = re.sub(pattern, replace_numbers, content)
    pattern = r'(\(\d+(\.\d+)? – \d+(\.\d+)?\))'
    new_content = re.sub(pattern, replace_numbers, new_content)
    pattern = r'(\(\d+(\.\d+)?–\d+(\.\d+)?\))'
    new_content = re.sub(pattern, replace_numbers, new_content)
    pattern = r'\d+(\.\d+)?'
    new_content = re.sub(pattern, replace_numbers, new_content)
    pattern = r'\d+'
    new_content = re.sub(pattern, replace_numbers, new_content)
    return new_content


for row in sheet.iter_rows():
    if type(row[0]) == openpyxl.cell.cell.Cell:
        all_content.append([cell.value for cell in row])
    else:
        all_content[-1][2] = all_content[-1][2] + " " + row[2].value
type1_list = []
type2_list = []
type3_list = []
contents = []

for row in all_content:
    assert len(row) == 4
    type1_list.append(row[0])
    type2_list.append(row[1])
    if row[3]:
        for i in row[3].split(","):
            if row[3]:
                type3_list += [i.strip() for i in row[3].split(",")]
                contents.append([change(row[2]), row[0], [i.strip() for i in row[3].split(",")]])
    else:
        contents.append([change(row[2]), row[0], ["None"]])

# 获得词缀类型、名称类型、属性类型
affix_list = list(set(type1_list))
name_list = list(set(type2_list))
property_list = list(set(type3_list))

# 定义字典
affix_table = {affix_list[i]: i for i in range(len(affix_list))}
property_table = {property_list[i]: i + 1 for i in range(len(property_list))}
property_table["None"] = 0
print(property_table)

# print(contents)
sentence_data = {}
states_list = []
new_contents = []

for row in contents:
    # print(row[1])
    # print(affix_table)
    state_list = [affix_table[row[1]]]
    id_list = []
    for j in row[2]:
        id_list.append(TypeTable.index(j))

    # print(id_list)
    one_hot_types = torch.sum(F.one_hot(torch.tensor(id_list), len(TypeTable)), dim=0)
    new_row = [row[0], row[1], row[2], one_hot_types]
    new_contents.append(new_row)

for row in new_contents:
    # print(row)
    seg_list = jieba.cut(row[0], cut_all=False)
    words = [i for i in list(seg_list) if i != " "] + ["<fin>"]
    row[0] = words
    # print(words)

# print(new_contents)
# # 原始字典
# dict_items = {
#     '召喚物': 1, '抗性': 2, '光環': 3, '混沌': 4,
#     '能量護盾': 5, '寶石': 6, '火焰': 7, '詛咒': 8,
#     '護甲': 9, '暴擊': 10, '冰冷': 11, '閃避': 12,
#     '物理': 13, '生命': 14, '攻擊': 15, '傷害': 16,
#     '魔力': 17, '元素': 18, '速度': 19, '閃電': 20,
#     '防禦': 21, '異常狀態': 22, '法術': 23, '能力': 24,
#     'None': 0
# }
# #16 13 15
# #2 6 10
# # 使用sorted()函数和lambda表达式对字典按照值排序
# sorted_items = sorted(dict_items.items(), key=lambda item: item[1])
# TypeTable  = ["prefix"] + [i[0] for i in sorted_items ]
# print(TypeTable)
